package com.xqboss.web.controller.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xqboss.apps.domain.sys.SysFeedbackRecord;
import com.xqboss.apps.dto.sys.WebSysFeedbackRecordDto;
import com.xqboss.apps.service.sys.SysFeedbackRecordService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 反馈记录接口
 *
 * @author wangcong
 * @date 2023-09-04
 */
@Api(tags = {"反馈记录"})
@RestController
@RequestMapping("/web/feedback")
public class WebSysFeedbackRecordController extends BaseController {
    @Autowired
    private SysFeedbackRecordService sysFeedbackRecordService;

    @PreAuthorize("@ss.hasPermi('web:feedback:list')")
    @ApiOperation("条件查询")
    @GetMapping("/list")
    public R<List<SysFeedbackRecord>> findBy(WebSysFeedbackRecordDto dto) {
        LambdaQueryWrapper<SysFeedbackRecord> wp = new LambdaQueryWrapper<>();
        if (ObjUtil.isNotNull(dto)) {
            if (StrUtil.isNotEmpty(dto.getKeyword())) {
                wp.eq(SysFeedbackRecord::getPhone, dto.getKeyword()).or();
                wp.eq(SysFeedbackRecord::getId, dto.getKeyword()).or();
                wp.like(SysFeedbackRecord::getContent, dto.getKeyword()).or();
                wp.like(SysFeedbackRecord::getReplyContent, dto.getKeyword());
            }
            if (ObjUtil.isNotNull(dto.getStartTime())) {
                wp.between(SysFeedbackRecord::getCreateTime, dto.getStartTime(), dto.getEndTime());
            }
            if (ObjUtil.isNotNull(dto.getStatus())) {
                wp.eq(SysFeedbackRecord::getStatus, dto.getStatus());
            }
        }
        wp.orderByDesc(SysFeedbackRecord::getCreateTime);
        dto.startPage();
        List<SysFeedbackRecord> list = sysFeedbackRecordService.list(wp);
        return R.ok(list);
    }

    @PreAuthorize("@ss.hasPermi('web:feedback:update')")
    @ApiOperation("修改反馈记录")
    @PutMapping("/update")
    @Log(title = "反馈记录", businessType = BusinessType.UPDATE)
    public R update(@RequestBody WebSysFeedbackRecordDto dto) {
        SysFeedbackRecord record = sysFeedbackRecordService.getById(dto.getId());
        Objects.requireNonNull(record, "请求的数据不存在");
        SysFeedbackRecord copied = BeanUtil.copyProperties(dto, SysFeedbackRecord.class);
        copied.setOptUserId(getUserId());
        copied.setOptUserName(getUsername());
        sysFeedbackRecordService.updateById(copied);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:feedback:quash')")
    @ApiOperation("撤销回复内容")
    @PutMapping("/quash")
    @Log(title = "反馈记录", businessType = BusinessType.UPDATE)
    public R quash(@RequestBody WebSysFeedbackRecordDto dto) {
        SysFeedbackRecord record = sysFeedbackRecordService.getById(dto.getId());
        if (ObjUtil.isNull(record)) {
            return R.fail("请求的数据不存在");
        }
        LambdaUpdateWrapper<SysFeedbackRecord> wq = new LambdaUpdateWrapper<>();
        wq.set(SysFeedbackRecord::getReplyContent, null);
        wq.set(SysFeedbackRecord::getStatus, 0);
        wq.set(SysFeedbackRecord::getOptUserId, null);
        wq.set(SysFeedbackRecord::getOptUserName, null);
        wq.eq(SysFeedbackRecord::getId, dto.getId());
        sysFeedbackRecordService.update(wq);
        return R.ok();
    }
}
